package com.wyj.longrangeservice.Util;


import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * author : 泥菩萨
 * time   : 2022/07/28
 */
public class WebSocketUtil {

    private static WebSocketUtil mWebSocketUtil;

    private OkHttpClient mOkHttpClient;

    private String url;

    private WebSocket websocket;

    private Callback mCallback;

    public void setmCallback(Callback mCallback) {
        this.mCallback = mCallback;
    }

    public static WebSocketUtil getWebSocketUtil(){

        if (mWebSocketUtil==null)mWebSocketUtil = new WebSocketUtil();

        return mWebSocketUtil;
    }

    private WebSocketUtil(){

        mOkHttpClient = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(10, TimeUnit.SECONDS)//设置写的超时时间
                .connectTimeout(10, TimeUnit.SECONDS)//设置连接超时时间
                .build();
    }

    /** 连接 */
    public void connect(String url){

        Request request = new Request.Builder().get().url(url).build();

        this.url = url;

        websocket = mOkHttpClient.newWebSocket(request, new WebSocketListener() {

            private Timer timer;

            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                super.onOpen(webSocket, response);

                if (mCallback!=null)mCallback.msg("连接中");
            }

            @Override
            public void onMessage(WebSocket webSocket, String text) {
                super.onMessage(webSocket, text);

                /** 连接成功开启心跳功能 */
                if ("1".equals(text)){

                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {

                            webSocket.send("0001");

                        }
                    },new Date(),1000);

                    if (mCallback!=null)mCallback.msg("连接成功");

                }else {

                    if (mCallback!=null)mCallback.getData(text);
                }
            }

            @Override
            public void onMessage(WebSocket webSocket, ByteString bytes) {
                super.onMessage(webSocket, bytes);

                if (mCallback!=null)mCallback.getData(bytes.toByteArray());
            }

            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                super.onClosed(webSocket, code, reason);

                //断开关掉心跳任务
                if (timer!=null)timer.cancel();
                if (mCallback!=null)mCallback.msg("连接关闭");
            }

            @Override
            public void onFailure(WebSocket webSocket, Throwable t, Response response) {
                super.onFailure(webSocket, t, response);

                //连接异常 重新连接
                connect(url);
                if (mCallback!=null)mCallback.msg("重新连接");
            }
        });


    }

    /** 发送 */
    public void send(String data){

        if (websocket!=null)websocket.send(data);
    }

    /** 发送 */
    public void send(ByteString data){

        if (websocket!=null)websocket.send(data);
    }

    /** 关闭 */
    public void cancel(){

        if (websocket!=null)websocket.cancel();
    }

    public interface Callback{

        void getData(byte[] data);

        void getData(String data);

        void msg(String data);
    }
}